<?php
declare (strict_types = 1);

namespace app\adminapi\controller;

use app\api\model\User as ModelUser;
use app\api\model\UserAuth;
use app\BaseController;
use think\facade\Db;
use think\App;

class SystemUser extends Common
{
    /**
     * 登录次数
     * @return [type] [description]
     */
    public function list()
    {
        $data = $this->_vali([
            'keyword.default' => '',
            'doctimeStart.default' => '',
            'doctimeEnd.default' => '',
        ],"POST");

        $where = [];

        if ($data['keyword']) {
            $where[] = ['short_name|company_name|contact_name|contact_phone','like',"%".$data['keyword']."%"];
        }

        if ($data['doctimeStart'] && $data['doctimeEnd']) {
            $where[] = ['create_time','between',[ strtotime($data['doctimeStart']),strtotime($data['doctimeEnd']." 23:59:59") ]];
        }

        $list = Db::name('system_user')->alias('u')
                ->join('role r','r.id = u.role_id','LEFT')
                ->order('u.id desc')
                ->field('u.id,u.username,r.role_name')
                ->where($where)
                ->paginate(10);

        $list = $list->toArray();

        return json(['code'=>200,'sum'=>$list['total'],'list'=>$list['data']]);
    }

    /**
     * 获取菜单
     * */
    public function getnum()
    {
        $getside = $this->getside();

        $map = [];
        if ($getside) {
            $map[] = ['id','in',$getside];
        }

        $list = Db::name('menu')->where($map)->field('id,title,pid,url as linkto,icon')->select();

        $map = [];
        foreach ($list as $key => $value) {
            if ($value['pid'] == 0) {
                $map[] = $value;
            }
        }

        foreach ($map as $key => $value) {
            foreach ($list as $k => $v) {
                if ($value['id'] == $v['pid']) {
                    $map[$key]['child'][] = $v;
                }
            }
        }

        return json(['code'=>200,'list'=>$map]);
    }

    /**
     * 设置状态
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function status()
    {
        $data = $this->_vali([
            'id.require' => 'id不能为空',
            'is_staff.require' => '状态不能为空',
        ],"POST");

        if ($data['id'] == 1) {
            return json(['code'=>0,'msg'=>'此管理员不可操作']);
        }
        $res = Db::name('data_user')->where('id',$data['id'])->update($data);
        if ($res) {
            return json(['code'=>200,'msg'=>'操作成功']);
        } else {
            return json(['code'=>0,'msg'=>'操作失败']);
        }
    }

    /**
     * 获取菜单权限
     * */
    public function getside()
    {

        if($this->role_id == 1) {
            return false;
        }

        $right_list = Db::name('role')->where('id',$this->role_id)->value('rule_list');

        if ($right_list) {
            $right_list = explode(",",$right_list);
        }

        // 查询二级菜单
        $top_id =  Db::name('menu')->where('id','in',$right_list)->group('pid')->column('pid');
        $right_list = array_unique(array_merge($right_list,$top_id));
        // 查询顶级
        $top_id =  Db::name('menu')->where('id','in',$top_id)->group('pid')->column('pid');
        $right_list = array_unique(array_merge($right_list,$top_id));

        return $right_list;
    }
}
